<template>
  <el-dialog title="实验室检查" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <table class="table-box">
      <thead>
        <tr>
          <th>
            <el-checkbox v-model="checkAll" @change="onCheckAll"></el-checkbox>
          </th>
          <th>检查项</th>
          <th>说明</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>
            <el-checkbox v-model="item.check"></el-checkbox>
          </td>
          <td>{{item.checkName}}</td>
          <td>{{item.remark}}</td>
        </tr>
      </tbody>
    </table>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/laboratory.html"></iframe>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "list", "patientId", "cureId"],
  data() {
    return {
      btnLoading: false,
      checkAll: true,
      tableList: []
    };
  },
  mounted() {
    let th = this;
  },
  methods: {
    async onSubmit() {
      let th = this;
      let arr = [];
      let json = {
        hospital: {
          name: th.adminUser.hospitalName,
          logo: th.adminUser.printLogo
        },
        professionalDeptName: th.$route.query.professionalDeptName,
        doctorName: th.$route.query.doctorName,
        patientInfo: null,
        time: new Date(),
        list: []
      };
      th.tableList.forEach(item => {
        let j = {
          name: item.checkName, 
          price: Number.parseFloat(item.price),
          remark: item.remark,
          checkupNum:item.checkupNum,
        };
        if (item.check) json.list.push(j);
      });
      if (json.list.length == 0) {
        th.$message.warning("请勾选数据");
        return;
      }
      let j = {
        patientId: th.patientId,
        cureId: th.cureId
      };
      th.btnLoading = true;
      //获取患者信息
      let res1 = await th.$api.getPatientDetail(j);
      th.btnLoading = false;
      if (!res1.executed) {
        return;
      }
      json.patientInfo = res1.data.patientInfo ? res1.data.patientInfo : {};
      //诊断
      json.firstVisit = res1.data.firstVisit;
      th.$refs["iframe1"].contentWindow.postMessage(
        json,
        `${window.location.origin}/print/laboratory.html`
      );
      th.onClose();
    },
    onCheckAll() {
      let th = this;
      th.tableList.forEach(item => {
        th.$set(item, "check", th.checkAll);
      });
    },
    getUnit(id) {
      let th = this;
      let str = "";
      let data = th.unitEnum.find(d => {
        return d.id == id;
      });
      if (data) {
        str = data.desc;
      }
      return str;
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        th.tableList = JSON.parse(JSON.stringify(th.list));
        th.onCheckAll();
      });
    },
    onClose() {
      let th = this;
      th.tableList = [];
      th.checkAll = true;
      th.$emit("input", false);
    }
  }
};
</script>